package edu.sctu.wangxiaoqiang.day20150607.timer;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.util.TimerTask;

public class Jishiqi extends JFrame {
    final TextField txt = new TextField(10);//TextField输入框// JFrame窗口类；
    JLabel lbl = new JLabel();              //对齐方式
    Date now = new Date();

    public Jishiqi() {

        now.setHours(0);//将时间初始化为0；
        now.setMinutes(0);
        now.setSeconds(0);

        final Timer timer = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Date now2 = new Date(now.getTime() + 1000);
                now = now2;
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                txt.setText(formatter.format(now));
                 if (now.equals("00:00:05")){
                     Timer timer1 = null;
                    timer1.restart();
                }
            }
        });

        JButton b1 = new JButton("开始计时");          //为按钮b1设置文字为“开始计时”
        JButton b2 = new JButton("停止计时");          //为按钮b2设置文字为“停止计时”

        //为按钮设置长宽
        b2.setBounds(40, 40, 40, 40);
        b1.setBounds(10, 10, 10, 10);
        txt.setBounds(110, 200, 80, 30);
        txt.setEditable(false);
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JButton b = (JButton) e.getSource();
                timer.start();                      //当点击按钮开始计时时 就开始计时
                b.setBackground(Color.yellow);      //设置按钮颜色黄色
            }
        });

        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                JButton b = (JButton) e.getSource();
                b.setLabel("停止计时");             //为按钮b2设置文字为“停止计时”
                timer.stop();                      //当点击按钮停止计时时 就停止计时
                b.setBackground(Color.yellow);     //设置按钮颜色黄色
            }
        });

        this.setLayout(new FlowLayout());
        this.add(txt);

        this.add(b2);
        this.add(b1);

        this.setSize(300, 200);                      //为窗口设置长宽
        setLocationRelativeTo(null);                 //居中显示
        this.setVisible(true);                       //把窗口设置为可见
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置退出选项卡

    }

    public static void main(String[] args) {
        Jishiqi t = new Jishiqi();
    }
}